//
//  XCTuiJianDetialViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/14.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCTuiJianDetialViewController.h"
#import "XCAFNetTool.h"
#import "XCUrlManger.h"
#import "XCTuiJianDetailModel.h"
#import "UIImageView+WebCache.h"
#import "XCListenMusicTableViewCell.h"
#import "OnlineAVplay.h"
#import "XCPlayViewController.h"

@interface XCTuiJianDetialViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, copy) NSString *albumId;
@property (nonatomic, retain) UIImageView *headView;
@property (nonatomic, copy) NSString *picUrl;
@property (nonatomic, retain) NSMutableArray *mutableArraySongName;
@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;
@property (nonatomic, retain) NSMutableArray *nameArray;
@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;

@end

@implementation XCTuiJianDetialViewController
- (instancetype)initwithAlbumId:(NSString *)albumId
{
    self = [super init];
    if (self) {
        self.albumId = albumId;
    }
    return self;
}
- (void)dealloc {
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
    
    [_tableView release];
    [_albumId release];
    [_mutableArraySongName release];
    [_mutableHeadIVPicurl release];
    [_picUrl release];
    [_headView release];
    [_nameArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"推荐音乐";
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    [self TuiJianDetailRequest];
// tableView 创建
    [self tableViewBuild];
    self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
    self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
    self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
    
}
#pragma mark - 网络请求
- (void)TuiJianDetailRequest {
    [XCAFNetTool GETNetWithUrl:[XCUrlManger TuiJianClickRequest:self.albumId] body:nil headerFile:nil response:XCJSON success:^(id result) {

        XCTuiJianDetailModel *tuiJianDetailModel = [[XCTuiJianDetailModel alloc] initWithDataSource:result[@"data"]];

        for (XCSongListModel *songListModel in tuiJianDetailModel.songList) {

            [self.dataSource addObject:songListModel];
        }

        for (XCSongListModel *songListModel in tuiJianDetailModel.songList) {
            if (songListModel.urlList.count != 0) {
                [self.mutableArraySongName addObject:[songListModel.urlList[0] objectForKey:@"url"]];
                [self.mutableHeadIVPicurl addObject:songListModel.picUrl];
                [self.mutableArraySingerNAme addObject:songListModel.singerName];
                [self.nameArray addObject:songListModel.name];
            }
            [songListModel release];

        }

        
        [self.headView sd_setImageWithURL:[NSURL URLWithString:tuiJianDetailModel.picUrl]placeholderImage:[UIImage imageNamed:@"Image-p"]];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark - tableViewBuild

- (void)tableViewBuild {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[XCListenMusicTableViewCell class] forCellReuseIdentifier:@"Identifier"];
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
//  头视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 2 - 80)];

    self.headView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, wide, high / 2 + 50)];

    [view addSubview:self.headView];
    
    self.tableView.tableHeaderView = view;
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat scale= 1-(scrollView.contentOffset.y/self.headView.bounds.size.height / 5);
    scale = (scale>=1)?scale :1;
    self.headView.transform = CGAffineTransformMakeScale(scale, scale);
     [self.navigationController.navigationBar setBackgroundImage:[self imageWithBgColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:self.tableView.contentOffset.y / 300]] forBarMetrics:UIBarMetricsDefault];
}

-(UIImage *)imageWithBgColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCListenMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];

    cell.songListModel = self.dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.width / 5 + 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
    
    [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
    [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
    [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
    [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
    
    XCSongListModel *songListModel = self.dataSource[indexPath.row];
    [[OnlineAVplay shareAVPlayer] playWithUrl:[songListModel.urlList[0] objectForKey:@"url"]];
    
    [[OnlineAVplay shareAVPlayer] play];
    [OnlineAVplay shareAVPlayer].isPlay = YES;
    
    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
    }];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
